home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / TGCBOR20.ARJ / INTROPAK.COM / TEGLVIEW.C < prev    next >
Text File  |  1991-06-12  |  15KB  |  604 lines

  1. /*---------------------------------------------------------------------------*
  2.  |               TEGL Windows ToolKit II                 |
  3.  |          Copyright (C) 1990, TEGL Systems Corporation             |
  4.  |                All Rights Reserved.                 |
  5.  *---------------------------------------------------------------------------*/
  6.  
  7. /* #include "teglsys.h" */
  8. #include <teglcond.h>
  9. #include <tgraph.h>
  10. #include <fastgrph.h>
  11. #include <teglintr.h>
  12. #include <fontsys.h>
  13. #include <virtmem.h>
  14. #include <ipstacks.h>
  15. #include <teglunit.h>
  16. #include <teglmain.h>
  17. #include <errorlog.h>
  18. #include <teglmenu.h>
  19. #include <selectfl.h>
  20. #include <sensems.h>
  21. #include <videochk.h>
  22. #include <iconsys.h>
  23. #include <teglspec.h>
  24. #include <teglfsio.h>
  25.  
  26. #ifndef MSDOS
  27. #include <dir.h>
  28. #include <alloc.h>
  29. #endif
  30.  
  31. #include <dos.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35.  
  36. #define maxlstrarr  16382     /*  -- MaxInt DIV 2  */
  37.  
  38. typedef char *lstr;         /*  -- pointer to char string */
  39.  
  40. typedef lstr lstrarr[maxlstrarr];
  41.  
  42. typedef struct fileviewrec *fileviewptr;
  43. typedef struct fileviewrec {
  44.     fileviewptr nextfileview;
  45.     imagestkptr fs;
  46.     lstrarr *data;
  47.     int lastline;
  48.     int offset;
  49.     int coloff;
  50.     unsigned maxrow;
  51.     unsigned maxbitlen;
  52.     unsigned divisons;
  53.     char filename[71];
  54.     fontptr fonttable;
  55. } fileviewrec;
  56.  
  57. #define arraymax 2000
  58.  
  59. fileviewptr anchorfileview = NULL;
  60. optionmptr om1, om2, om3;
  61. char path[80];
  62.  
  63. unsigned exitoption(imagestkptr frame, msclickptr mouseclickpos)
  64. {
  65.     logmessage("Thank you for using our TEGL View Program...");
  66.     abortexit("The source code for this program is included with the TEGL Windows Toolkit II");
  67.     return 0;
  68. }
  69.  
  70. unsigned infooption(imagestkptr frame, msclickptr mouseclickpos)
  71. {
  72.     unsigned x, y, x1, y1;
  73.  
  74.     if (visualbuttonpress(frame, mouseclickpos)) {
  75.     resetframeactive(frame->lstptr, FALSE);
  76.  
  77.     x = 160;
  78.     y = 120;
  79.     x1 = x + 360;
  80.     y1 = y + 170;
  81.  
  82.     explodefrommsclick(frame,mouseclickpos,x,y,x1,y1);
  83.  
  84.     prepareforupdate(stackptr);
  85.  
  86.     setshadowcolor(WHITE);
  87.     shadowbox(x,y,x1,y1);
  88.     setcolor(BLACK);
  89.     rectangle(x+3,y+3,x+355,y+165);
  90.     rectangle(x+5,y+5,x+353,y+163);
  91.  
  92.     settextjustify(CENTER_TEXT,TOP_TEXT);
  93.  
  94.     setshadowtexttype(wndwx19);    /* 5 */
  95.     shadowtext(x+190,y+10,GREEN,"TEGLVIEW");
  96.     setshadowtexttype(NULL);
  97.  
  98.     setteglfont(font14);
  99.     extendtextxy(x+190,y+65,"Copyright (c) 1990,");
  100.     extendtextxy(x+190,y+79,"TEGL Systems Corporation");
  101.     extendtextxy(x+190,y+93,"All Rights Reserved");
  102.     extendtextxy(x+190,y+120,"Written and Designed by Richard Tom");
  103.  
  104.     settextjustify(LEFT_TEXT,TOP_TEXT);
  105.  
  106.     commitupdate();
  107.  
  108.     definebuttonclick(stackptr,x+300,y+143,imageOK,collapsetomsclick);
  109.  
  110.     setmouseposition(x+310,y+153);
  111.     }
  112.     return 0;
  113. }
  114.  
  115. fileviewptr findfileview(imagestkptr frame)
  116. {
  117.     fileviewptr fvptr;
  118.  
  119.     fvptr = anchorfileview;
  120.     while ((fvptr != NULL) && (fvptr->fs != frame))
  121.     fvptr = fvptr->nextfileview;
  122.  
  123.     return fvptr;
  124. }
  125.  
  126. void pageslideposition(fileviewptr fvptr)
  127. {
  128.     unsigned x, y;
  129.  
  130.     x = fvptr->fs->x1 - 23;
  131.     y = fvptr->fs->y+((fvptr->offset/fvptr->maxrow)*fvptr->divisons)+
  132.     ((((fvptr->offset%fvptr->maxrow)*fvptr->divisons)+
  133.     (fvptr->maxrow/2))/fvptr->maxrow)+16+23;
  134.  
  135.     setslideposition(fvptr->fs, x, y);
  136. }
  137.  
  138. char *fittext(char far * mystr, fileviewptr fvptr, unsigned linenum, lstrarr mydata)
  139. {
  140.     unsigned mysize;
  141.     unsigned mylen;
  142.  
  143.     setteglfont(fvptr->fonttable);
  144.     strcpy(mystr,&mydata[linenum][fvptr->coloff]);
  145.     mysize = tegltextwidth(mystr);
  146.     mylen = strlen(mystr);
  147.     while (mysize > fvptr->maxbitlen) {
  148.     mysize -= teglcharwidth(mystr[mylen - 1]);
  149.     mystr[mylen-1] = '\0';
  150.     mylen--;
  151.     }
  152.  
  153.     while (strchr(mystr,'\t') != NULL)
  154.      *strchr(mystr,'\t') = ' ';
  155.  
  156.     return mystr;
  157. }
  158.  
  159. void display(fileviewptr fvptr)
  160. {
  161.     int i;
  162.     unsigned x, y, x1, y1;
  163.     imagestkptr fs;
  164.     char mystr[255];
  165.  
  166.     setteglfont(fvptr->fonttable);
  167.  
  168.     fs = fvptr->fs;
  169.     getmargins(fs,&x,&y,&x1,&y1);
  170.  
  171.     gotofsxy(fs, x, y);
  172.     prepareforupdate(fs);
  173.     bar(fs->x+x,fs->y+y,fs->x+x1,fs->y+y1);
  174.     for (i=1; (i<=fvptr->maxrow && fvptr->offset+i<= fvptr->lastline); i++)
  175.     outtegltextxy(fs->x+x,fs->y+y+(i-1)*(teglcharheight()-2),
  176.              fittext(mystr,fvptr,fvptr->offset+i,*fvptr->data));
  177.     commitupdate();
  178. }
  179.  
  180. unsigned pagedown(imagestkptr fs, msclickptr mouseclickpos)
  181. {
  182.     int tempoff;
  183.     fileviewptr fvptr;
  184.  
  185.     fvptr = findfileview(fs);
  186.  
  187.     tempoff = fvptr->offset;
  188.     tempoff += fvptr->maxrow-1;
  189.     if (tempoff>fvptr->lastline-fvptr->maxrow)
  190.     tempoff=fvptr->lastline-fvptr->maxrow;
  191.  
  192.     if (tempoff != fvptr->offset) {
  193.     fvptr->offset = tempoff;
  194.     display(fvptr);
  195.     pageslideposition(fvptr);
  196.     } else {
  197.     beep(200,1,10);
  198.     while (mouse_buttons) {}
  199.     }
  200.  
  201.     return 1;
  202. }
  203.  
  204. unsigned pageup(imagestkptr fs, msclickptr mouseclickpos)
  205. {
  206.     int tempoff;
  207.     fileviewptr fvptr;
  208.  
  209.     fvptr = findfileview(fs);
  210.  
  211.     tempoff = fvptr->offset;
  212.     tempoff -= fvptr->maxrow - 1;
  213.     if (tempoff < 0)
  214.     tempoff = 0;
  215.  
  216.     if (tempoff != fvptr->offset) {
  217.     fvptr->offset = tempoff;
  218.     display(fvptr);
  219.     pageslideposition(fvptr);
  220.     } else {
  221.     beep(800,1,10);
  222.     while (mouse_buttons) {}
  223.     }
  224.  
  225.     return 1;
  226. }
  227.  
  228. unsigned curup(imagestkptr fs, msclickptr mouseclickpos)
  229. {
  230.     fileviewptr fvptr;
  231.     unsigned x, y, x1, y1;
  232.     char mystr[255];
  233.  
  234.     fvptr = findfileview(fs);
  235.     setteglfont(fvptr->fonttable);
  236.     getmargins(fs,&x,&y,&x1,&y1);
  237.  
  238.     fvptr->offset--;
  239.     if (fvptr->offset < 0)
  240.     fvptr->offset = 0;
  241.     else {
  242.     scrolldown(fs);
  243.     prepareforpartialupdate(fs,fs->x+x,fs->y+y,fs->x+x1,fs->y+y+teglcharheight());
  244.     outtegltextxy(fs->x+x,fs->y+y,fittext(mystr,fvptr,fvptr->offset+1,*fvptr->data));
  245.     commitupdate();
  246.     pageslideposition(fvptr);
  247.     }
  248.     return 1;
  249. }
  250.  
  251. unsigned curdown(imagestkptr fs, msclickptr mouseclickpos)
  252. {
  253.     fileviewptr fvptr;
  254.     unsigned x, y, x1, y1;
  255.     char mystr[255];
  256.  
  257.     fvptr = findfileview(fs);
  258.     setteglfont(fvptr->fonttable);
  259.     getmargins(fs,&x,&y,&x1,&y1);
  260.  
  261.     fvptr->offset++;
  262.     if (fvptr->offset>fvptr->lastline-fvptr->maxrow)
  263.     fvptr->offset = fvptr->lastline-fvptr->maxrow;
  264.     else {
  265.     scrollup(fs);
  266.     prepareforpartialupdate(fs,fs->x+x,fs->y+y1-(teglcharheight()-2),fs->x+x1,fs->y+y1);
  267.     outtegltextxy(fs->x+x,fs->y+y1-(teglcharheight()-2),fittext(mystr,fvptr,fvptr->offset+fvptr->maxrow,*fvptr->data));
  268.     commitupdate();
  269.     pageslideposition(fvptr);
  270.     }
  271.     return 1;
  272. }
  273.  
  274. unsigned curleft(imagestkptr fs, msclickptr mouseclickpos)
  275. {
  276.     fileviewptr fvptr;
  277.  
  278.     fvptr = findfileview(fs);
  279.     setteglfont(fvptr->fonttable);
  280.  
  281.     fvptr->coloff++;
  282.     if (fvptr->coloff > 100)
  283.     fvptr->coloff = 100;
  284.     display(fvptr);
  285.     return 1;
  286. }
  287.  
  288. unsigned curright(imagestkptr fs, msclickptr mouseclickpos)
  289. {
  290.     fileviewptr fvptr;
  291.  
  292.     fvptr = findfileview(fs);
  293.     setteglfont(fvptr->fonttable);
  294.  
  295.     fvptr->coloff--;
  296.     if (fvptr->coloff < 0)
  297.     fvptr->coloff = 0;
  298.     display(fvptr);
  299.     return 1;
  300. }
  301.  
  302. void freelstr(lstr * s)
  303. {
  304.     if (*s != NULL)
  305.     cfreemem(*s,(unsigned long) strlen(*s)+1);
  306.     *s = NULL;
  307. }
  308.  
  309. void allocatelstrarr(lstrarr **data,unsigned limit)
  310. {
  311.     *data = (lstrarr *) cgetmem((unsigned long) (limit+1)*4);  /*  -- array is 0 .. limit  */
  312.     memset(*data,0,(limit+1)*4);        /* clear array */
  313. }
  314.  
  315. void disposelstrarr(lstrarr data,unsigned limit)
  316. {
  317.     unsigned  count;
  318.  
  319.     for (count = 0; count <= limit; count++)   /*  -- send off pointers to free  */
  320.     freelstr(&data[count]);         /*  -- up memory  */
  321.  
  322.     cfreemem(data,(unsigned long) (limit+1)*4);
  323.     *data = NULL;
  324. }
  325.  
  326. void store(lstr * s, char * s2)
  327.   {
  328.     freelstr(s);               /*  -- free up memory already used  */
  329.     *s = (lstr) cgetmem((unsigned long) strlen(s2)+1); /*  -- get memory to store new string  */
  330.     strcpy(*s,s2);
  331.   }
  332.  
  333. char readfilelstr(char * filename, int * lastline, int limit, lstrarr data)
  334. {
  335.     FILE     *inputfile;
  336.     char     inputline[255];
  337.     char     readok;
  338.  
  339.     readok = TRUE;
  340.     *lastline = 0;
  341.  
  342.     if ((inputfile=fopen(filename,"rt")) != NULL) {
  343.  
  344.      fgets(inputline,255,inputfile);
  345.      do {
  346.           if (strstr(inputline,"\n") != NULL)
  347.          *strstr(inputline,"\n")='\0';
  348.  
  349.           if (strstr(inputline,"\r") != NULL)
  350.          *strstr(inputline,"\r")='\0';
  351.  
  352.           (*lastline)++;   /*  -- inc line counter    */
  353.  
  354.           store(&data[*lastline],inputline);   /*  -- allocate mem and store the string  */
  355.  
  356.           fgets(inputline,255,inputfile);
  357.      } while (!feof(inputfile) && (*lastline != limit));
  358.     }
  359.  
  360.     if (*lastline == limit || inputfile==NULL) /*  -- out of line space  */
  361.       {
  362.     readok = FALSE;
  363.     disposelstrarr(data,limit);   /*  -- free up heap  */
  364.     *lastline = 0;              /*  -- reset upper bound    */
  365.       }
  366.  
  367.     fclose(inputfile);
  368.     return readok;
  369.   }
  370.  
  371. void dropfileview(imagestkptr fs)
  372. {
  373.     fileviewptr nfvptr, lfvptr;
  374.  
  375.     lfvptr = (fileviewptr) NULL;
  376.     nfvptr = anchorfileview;
  377.     while ((nfvptr != NULL) && (nfvptr->fs != fs)) {
  378.     lfvptr = nfvptr;
  379.     nfvptr = nfvptr->nextfileview;
  380.     }
  381.  
  382.     if (nfvptr != NULL) {
  383.     if (lfvptr == NULL)
  384.         anchorfileview = nfvptr->nextfileview;
  385.     else
  386.         lfvptr->nextfileview = nfvptr->nextfileview;
  387.  
  388.     cfreemem(nfvptr,(unsigned long) sizeof(*nfvptr));
  389.     }
  390. }
  391.  
  392. unsigned docquit(imagestkptr fs, msclickptr mouseclickpos)
  393. {
  394.     fileviewptr fvptr;
  395.  
  396.     fvptr = findfileview(fs);
  397.     disposelstrarr(*(fvptr->data), arraymax);
  398.     closefsio(fs);
  399.     dropfileview(fs);
  400.     dropsliders(fs);
  401.     dropstackimage(fs);
  402.     return 1;
  403. }
  404.  
  405. unsigned slidedoc(imagestkptr fs, msclickptr mouseclickpos)
  406. {
  407.     fileviewptr fvptr;
  408.     sliderptr slptr;
  409.     unsigned diff;
  410.     int tempoff;
  411.  
  412.     fvptr = findfileview(fs);
  413.     slptr = findsliderfs(fs, mouseclickpos);
  414.     if (slptr == NULL)
  415.     aborterror("Unable to located Slider Information.",240);
  416.  
  417.     diff = (int) ((mouseclickpos->ms.y - slptr->miny));
  418.     tempoff = ((diff/fvptr->divisons)*fvptr->maxrow)+(diff%fvptr->divisons);
  419.  
  420.     if (tempoff > fvptr->lastline - fvptr->maxrow)
  421.     tempoff = fvptr->lastline - fvptr->maxrow;
  422.  
  423.     if (tempoff != fvptr->offset) {
  424.     fvptr->offset = tempoff;
  425.     display(fvptr);
  426.     } else {
  427.     beep(800, 1, 10);
  428.     while (mouse_buttons) {}
  429.     }
  430.  
  431.     return 1;
  432. }
  433.  
  434. unsigned docwindow(imagestkptr fs, msclickptr mouseclickpos)
  435. {
  436.     unsigned x, y, x1, y1;
  437.     unsigned pages;
  438.     unsigned barlength;
  439.     fileviewptr fvptr;
  440.  
  441.     resetmouseclicks(fs, NULL);
  442.     closefsio(fs);
  443.     dropsliders(fs);
  444.     fvptr = findfileview(fs);
  445.  
  446.     x = fs->x;
  447.     y = fs->y;
  448.     x1 = fs->x1;
  449.     y1 = fs->y1;
  450.  
  451.     prepareforupdate(fs);
  452.     setshadowcolor(WHITE);
  453.     shadowbox(x, y, x1, y1);
  454.  
  455.     setteglfont(fvptr->fonttable);
  456.  
  457.     fvptr->maxbitlen=x1-x-40;
  458.     fvptr->maxrow=((y1-y-55)/(teglcharheight()-2));
  459.  
  460.     /* Top area of window */
  461.     setshadowcolor(LIGHTGRAY);
  462.     shadowbox(x,y,x+20,y+18);
  463.     teglwrtchar(28,x+5,y+3,BLACK,WHITE);
  464.     definemouseclickarea(fs,0,0,20,18,TRUE,docquit,MSCLICK);
  465.  
  466.     shadowbox(x+18,y,x1-22,y+18);
  467.     definemouseclickarea(fs,18,0,x1-x-22,18,TRUE,selectandmoveframe,MSCLICK);
  468.  
  469.     shadowbox(x1-24,y,x1,y+18);
  470.     defineresizeclickarea(fs,x1-x-24,0,x1-x,18,docwindow);
  471.  
  472.     /* right area of window */
  473.     /* Curup */
  474.     shadowbox(x1-24,y+16,x1,y+16+24);
  475.     definemouseclickarea(fs,x1-x-24,16,x1-x,20,TRUE,curup,MSCLICK);
  476.     definemouseclickarea(fs,x1-x-24,20,x1-x,20+20,TRUE,pageup,MSCLICK);
  477.     putpict(x1-22,y+18,imageUP,BLACK);
  478.  
  479.     setshadowfillstyle(CLOSE_DOT_FILL);
  480.     shadowbox(x1-24,y+16+22,x1,y1-16-22);
  481.     setshadowfillstyle(SOLID_FILL);
  482.  
  483.     /* Curdown */
  484.     shadowbox(x1-24,y1-16-24,x1,y1-16);
  485.     definemouseclickarea(fs,x1-x-24,y1-y-16-24,x1-x,y1-y-20,TRUE,pagedown,MSCLICK);
  486.     definemouseclickarea(fs,x1-x-24,y1-y-20,x1-x,y1-y-16,TRUE,curdown,MSCLICK);
  487.     putpict(x1-22,y1-16-22,imageDOWN,BLACK);
  488.  
  489.     /* bottom area of window */
  490.     shadowbox(x,y1-18,x+20,y1);
  491.     defineresizeclickarea(fs,0,y1-y-18,20,y1-y,docwindow);
  492.  
  493.     setshadowfillstyle(CLOSE_DOT_FILL);
  494.     shadowbox(x+18,y1-18,x1-22,y1);
  495.     setshadowfillstyle(SOLID_FILL);
  496.     /* DefineMouseClickArea(FS,18,y1-y-18,x1-x-22,y1-y,TRUE,NULL,MSCLICK); */
  497.  
  498.     shadowbox(x1-24,y1-18,x1,y1);
  499.     defineresizeclickarea(fs,x1-x-24,y1-y-18,x1-x,y1-y,docwindow);
  500.     commitupdate();
  501.  
  502.  
  503.     /* Scroll Slider */
  504.     pages = arraymax / fvptr->maxrow;
  505.     barlength=((y1-16-25)-(y+16+23));
  506.     fvptr->divisons = barlength/pages;
  507.     barlength = barlength-(((fvptr->lastline+fvptr->maxrow-1)/fvptr->maxrow)*fvptr->divisons);
  508.  
  509.     definesliderarea(fs,x1-x-24,16+23,x1-x,16+23+barlength,x1-x-24,16+23,x1-x,y1-y-16-25,slidedoc);
  510.     prepareforupdate(fs);
  511.     setshadowcolor(WHITE);
  512.     setfillstyle(SOLID_FILL, WHITE);
  513.     bar(x1-24,y+16+23,x1-3,y+16+23+barlength);
  514.     setcolor(BLACK);
  515.     rectangle(x1-24,y+16+23,x1-3,y+16+23+barlength);
  516.     commitupdate();
  517.  
  518.     defineresizeminmax(fs,90,90,639,349);
  519.  
  520.     openfsio(fs,10,30,x1-x-30,30+(fvptr->maxrow*(teglcharheight()-2)));
  521.  
  522.     display(fvptr);
  523.     return 1;
  524. }
  525.  
  526. void createviewfile(char *s)
  527. {
  528.     unsigned x, y, x1, y1;
  529.     fileviewptr fvptr;
  530.  
  531.     fvptr = (fileviewptr) cgetmem((unsigned long) sizeof(*fvptr));
  532.  
  533.     strcpy(fvptr->filename, s);
  534.     fvptr->fonttable = light14;
  535.     fvptr->offset = 0;
  536.     fvptr->coloff = 0;
  537.  
  538.     allocatelstrarr(&fvptr->data, arraymax);
  539.     if (readfilelstr(s, &fvptr->lastline, arraymax,*fvptr->data)) {
  540.     x = 15;
  541.     y = 25;
  542.     x1 = 624;
  543.     y1 = 334;
  544.     pushimage(x,y,x1,y1);
  545.     fvptr->fs = stackptr;
  546.     fvptr->nextfileview = anchorfileview;
  547.     anchorfileview = fvptr;
  548.  
  549.     docwindow(fvptr->fs, NULL);
  550.     } else
  551.     cfreemem(fvptr,(unsigned long) sizeof(*fvptr));
  552.  
  553. }
  554.  
  555. unsigned fileselectandview(imagestkptr frame, msclickptr mouseclickpos)
  556. {
  557.     char selected;
  558.     char selectedfile[80];
  559.     unsigned x, y;
  560.  
  561.     x = 100;
  562.     y = 100;
  563.     if (frame != NULL) {
  564.     x = frame->x;
  565.     y = frame->y;
  566.     }
  567.     selected = selectafile(x, y, path, selectedfile);
  568.  
  569.     if (selected)
  570.     createviewfile(selectedfile);
  571.  
  572.     return 1;
  573. }
  574.  
  575. void main(int argc, char **argv)
  576. {
  577.     easytegl();
  578.  
  579.     setautorotate(TRUE);
  580.  
  581.     getcwd(path, sizeof(path));
  582.     strcat(path, "\\*.*");
  583.  
  584.     om1 = createoptionmenu(font14);
  585.     defineoptions(om1, "TEGLView ~I~nfo...", TRUE, infooption);
  586.  
  587.     om2 = createoptionmenu(font14);
  588.     defineoptions(om2, "~O~pen ", TRUE, fileselectandview);
  589.     defineoptions(om2, "--", FALSE, NULL);
  590.     defineoptions(om2, "~Q~uit", TRUE, exitoption);
  591.  
  592.     setteglfont(font14);
  593.     createbarmenu(0, 0, getmaxx());
  594.     setbarmenumargin(24);
  595.     outbaroption(" ~D~esk  ", om1);
  596.     outbaroption(" ~F~ile  ", om2);
  597.  
  598.     putpict(5, 1, imageTINYTEGL, BLACK);
  599.  
  600.     setctrlbreakfs(exitoption);
  601.  
  602.     teglsupervisor();
  603. }
  604.